package com.hogocloud.myframe.adapter;

import android.animation.ObjectAnimator;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.hogocloud.corelibrary.utils.DeviceUtils;
import com.hogocloud.corelibrary.utils.StatusBarUtils;
import com.hogocloud.myframe.R;
import com.hogocloud.myframe.data.bean.SelectItemBean;
import com.hogocloud.myframe.databinding.ItemSelectTopBinding;

import java.util.List;

/**
 * @author GaoJunchao
 * @date 2020-04-15
 */
public class ReadDragAdapter extends RecyclerView.Adapter<ReadDragAdapter.ViewHolder> {

    private List<SelectItemBean> mData;
    private int mSpan;
    private OnItemTouchListener mListener;

    public void setData(List<SelectItemBean> data, int i) {
        if (null != data) {
            this.mData = data;
            this.mSpan = i;
            notifyDataSetChanged();
        }
    }

    public void playErrorAnim(View view) {
        ObjectAnimator.ofFloat(view, "translationX", 0, 8, -8, 8, 0)
                .setDuration(500)
                .start();
//        Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
////        translateAnimation.setInterpolator(new LinearInterpolator());
//        translateAnimation.setRepeatCount(2);
//        translateAnimation.setRepeatMode(Animation.REVERSE);
//        translateAnimation.setDuration(200);
//        view.setAnimation(translateAnimation);
//        translateAnimation.start();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemSelectTopBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_select_top, parent, false);
        return new ViewHolder(binding.getRoot(), binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (null == mData || null == mData.get(position)) return;
        SelectItemBean bean = mData.get(position);

        holder.mBinding.ivBottom.setImageResource(bean.getBottomPic());
        holder.mBinding.ivTop.setImageResource(bean.getTopPic());
        holder.mBinding.ivBottom.setVisibility(bean.isVisible() ? View.VISIBLE : View.INVISIBLE);
        holder.mBinding.ivTop.setVisibility(bean.isVisible() ? View.VISIBLE : View.INVISIBLE);

        if (bean.getPlayErrorAnim()) {
            playErrorAnim(holder.mBinding.ivBottom);
            bean.setPlayErrorAnim(false);
        }

        if (position < mSpan) { //第一排
            if (null != mListener) {
                holder.mBinding.ivBottom.setOnTouchListener((v, event) -> {
                    mListener.onItemTouch(holder);
                    return true;
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemSelectTopBinding mBinding;

        public ViewHolder(@NonNull View itemView, ItemSelectTopBinding binding) {
            super(itemView);
            this.mBinding = binding;

            ViewGroup.LayoutParams layoutParams = binding.getRoot().getLayoutParams();
            int barHeight = StatusBarUtils.INSTANCE.getStatusBarHeight(itemView.getContext());
            int screenHeight = DeviceUtils.INSTANCE.getScreenHeight(itemView.getContext());
            layoutParams.height = (screenHeight - barHeight) / 2;
            binding.getRoot().requestLayout();
        }
    }

    public interface OnItemTouchListener {
        void onItemTouch(ViewHolder holder);
    }

    public void addOnItemTouchListener(OnItemTouchListener listener) {
        this.mListener = listener;
    }
}
